//
//  UIToolbarViewController.m
//  UITest
//
//  Created by wp w on 12-6-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIToolbarViewController.h"

@interface UIToolbarViewController ()

@end

@implementation UIToolbarViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)buttonClick:(id)sender {
	
	NSLog(@"you clicked button: %@",[sender title]);
}

- (void)viewDidLoad {
	
	[super viewDidLoad];
	
    //设置UIToolbar的frame
	CGSize viewSize = self.view.frame.size;
	float toolbarHeight = 44.0;
	CGRect toolbarFrame = CGRectMake(0,viewSize.height-toolbarHeight,viewSize.width,toolbarHeight);
    
    //新建UIToolbar
	UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
	
    //设置大小和位置变化时该怎么办
	myToolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth 
    | 
    UIViewAutoresizingFlexibleLeftMargin 
    | 
    UIViewAutoresizingFlexibleRightMargin 
    | 
    UIViewAutoresizingFlexibleTopMargin;
    
    //设置颜色和透明度
    myToolbar.tintColor = [UIColor redColor];
    myToolbar.translucent = YES;
	
	UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithTitle:@"button 1" 
																style:UIBarButtonItemStylePlain target:self 
															   action:@selector(buttonClick:)];
	
	UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithTitle:@"button 2" 
																style:UIBarButtonItemStyleBordered 
															   target:self 
															   action:@selector(buttonClick:)];
	
	UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"apple_icon.png"] 
																style:UIBarButtonItemStyleBordered 
															   target:self 
															   action:@selector(buttonClick:)];
	
    //UIBarButtonSystemItemFlexibleSpace,强制按钮位于工具栏的某一端
    //UIBarButtonSystemItemFixedSpace,只是在按钮之间添加空间
	UIBarButtonItem *flexButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
																				target:nil 
																				action:nil];
	
	UIBarButtonItem *trashButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash 
																				 target:self 
																				 action:@selector(buttonClick:)];
	
	NSArray *buttons = [[NSArray alloc] initWithObjects:button1,button2,button3, flexButton,trashButton,nil];
	
	//cleanup
	[button1 release];
	[button2 release];
	[button3 release];
	[flexButton release];
	[trashButton release];
	
	[myToolbar setItems:buttons animated:NO];
    
	[buttons release];
	
	[self.view addSubview:myToolbar];
	
	[myToolbar release];	
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
